package com.hongxun.design.adapters;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hongxun.design.R;
import com.hongxun.design.bean.NextDoorBean;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.ui.dialogstyle.MyDialog;

import java.util.List;

/**
 * Created by zerdoor_pc on 2016/5/23.
 */
public class RadioInterfaceAdapter extends BaseAdapter {
    List<NextDoorBean> data;
    Context context;
    //    public Map<Integer, Boolean> map;
    LayoutInflater inflater;

    public int select = -1;

    public RadioInterfaceAdapter(List<NextDoorBean> data, Context context) {
        this.context = context;
        this.data = data;
        updata();
        inflater = LayoutInflater.from(context);
    }


    public void updata() {
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).is_check) {
                if (data.get(i).text.length() == 0) {
                    data.get(i).is_check = false;
                } else {
                    select = i;
                    break;
                }
            }
        }
    }

    public List<NextDoorBean> getData() {
        return data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        NextDoorBean bean = data.get(i);
        Hollder hollder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.listview_item_radiointerface, null);
            hollder = new Hollder(view);
            view.setTag(hollder);
        } else {
            hollder = (Hollder) view.getTag();
        }
        if (bean.text.trim().length() == 0) {
            hollder._title.setText(R.string.input_name);
        } else {
            hollder._title.setText(bean.text);
        }
//        hollder._button.setOnClickListener(new MClickListener(i, bean) {
//            @Override
//            public void onClick(View view, int position, Object msg) {
//                checkItem(position, msg);
//            }
//        });
        hollder._title.setOnClickListener(new MClickListener(i, hollder, bean) {
            @Override
            public void onClick(View view, int position, Object msg) {
                MyDialog.dialogForEdieText((Activity) context, msg, msg2);
            }
        });

        hollder._checked.setOnClickListener(new MClickListener(i, bean) {
            @Override
            public void onClick(View view, int position, Object msg) {
                checkItem(position, msg);
            }
        });

        if (select == -1 && bean.text.trim().length()>0) {
            select = i;
            bean.is_check=true;
        }
        hollder._checked.setChecked(bean.is_check);
        return view;
    }

    private void checkItem(int position, Object msg) {
        NextDoorBean bean = (NextDoorBean) msg;
        if (bean.text.length() == 0) {
            bean.is_check = false;
            ToastShow.showShort(context, "选中项不能为空！");
            notifyDataSetChanged();
            return;
        } else {
            if (select != -1) {
                data.get(select).is_check = false;
            }
            if (bean.is_check) {
                bean.is_check = false;
            } else {
                bean.is_check = true;
                select = position;
            }
            notifyDataSetChanged();
        }


    }

    public class Hollder {
        public CheckBox _checked;
        public TextView _title;
        public RelativeLayout _button;
        public EditText _ed;

        Hollder(View view) {
            _button = (RelativeLayout) view.findViewById(R.id.radiointerface_button);
            _checked = (CheckBox) view.findViewById(R.id.radiointerface_checked);
            _title = (TextView) view.findViewById(R.id.radiointerface_value);

        }
    }


}
